package util;

public class ByteUtil {
	public static String byteToHexString(byte b) {
		String s = Integer.toHexString(b & 0xff);
		if (s.length() < 2) {
			s = "0" + s;
		}
		return s;
	}

	public static int byteToInt(byte b) {
		return (b & 0xff);
	}

	public static int getIndexBit(byte b, int index) {
		if (index >= 8) {
			return b & 0xff;
		} else {
			int a = (int) Math.pow(2, index);
			return (b & a) >> (index);
		}
	}

	public static int getFourBit(byte b, boolean isLow) {
		if (isLow) {
			return b & 0x0f;
		} else {
			return (b & 0xf0) >> 4;
		}
	}

	public static int unionByte(byte b1, byte b2) {
		byte c1 = 0, c2 = 0;
		int a = 0;
		a = (((c1 & 0xff) << 24) | ((c2 & 0xff) << 16) | ((b1 & 0xff) << 8) | ((b2 & 0xff) << 0));
		return a;
	}

	public static int unionInt(int b1, int b2) {
		return b1 << 4 | b2;
	}

	public static long unionByte(byte b1, byte b2, byte b3, byte b4) {
		return (((b1 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b3 & 0xff) << 8) | ((b4 & 0xff) << 0));
	}
}
